package by.yakusekmaxim.bookgid.webapp.page.admin.autor;

import javax.inject.Inject;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;

import by.yakusekmaxim.bookgid.dataaccess.util.Filter;
import by.yakusekmaxim.bookgid.datamodel.Author;
import by.yakusekmaxim.bookgid.datamodel.Author_;
import by.yakusekmaxim.bookgid.datamodel.enums.PublicationStatus;
import by.yakusekmaxim.bookgid.services.AuthorService;
import by.yakusekmaxim.bookgid.webapp.page.AdminLayout;

public class AuthorPage extends AdminLayout {

	@Inject
	private AuthorService authorService;

	private Filter<Author> filter = new Filter<Author>();

	protected void onInitialize() {
		super.onInitialize();

		add(new Link<Object>("linkEditAuthorPage") {
			@Override
			public void onClick() {
				setResponsePage(new EditAuthorPage(new Author()));
			}
		});

		Link<Object> countAll = (new Link<Object>("linkCountAll") {

			@Override
			public void onClick() {
				AuthorPage authorPage = new AuthorPage();
				setResponsePage(authorPage);
			}
		});
		countAll.setEnabled(authorService.getCount() != 0);
		countAll.add(new Label("countAll", authorService.getCount()));
		add(countAll);

		Link<Object> countEdited = (new Link<Object>("linkCountEdited") {

			@Override
			public void onClick() {
				filter.setEnabled(true);
				filter.setAttribute(Author_.status);
				filter.setValue(PublicationStatus.publicationstatus_edited);
				AuthorPage authorPage = new AuthorPage(filter,
						new ResourceModel("p.admin.author.pageName.allEdited"));
				setResponsePage(authorPage);
			}
		});
		countEdited.setEnabled(authorService.getCount(Author_.status,
				PublicationStatus.publicationstatus_edited) != 0);
		countEdited.add(new Label("countEdited", authorService.getCount(
				Author_.status, PublicationStatus.publicationstatus_edited)));
		add(countEdited);

		Link<Object> countValidation = (new Link<Object>("linkCountValidation") {

			@Override
			public void onClick() {
				filter.setEnabled(true);
				filter.setAttribute(Author_.status);
				filter.setValue(PublicationStatus.publicationstatus_validation);
				AuthorPage authorPage = new AuthorPage(filter,
						new ResourceModel(
								"p.admin.author.pageName.allValidation"));
				setResponsePage(authorPage);
			}
		});
		countValidation.setEnabled(authorService.getCount(Author_.status,
				PublicationStatus.publicationstatus_validation) != 0);
		countValidation.add(new Label("countValidation", authorService
				.getCount(Author_.status,
						PublicationStatus.publicationstatus_validation)));
		add(countValidation);

		Link<Object> countPublished = (new Link<Object>("linkCountPublished") {

			@Override
			public void onClick() {
				filter.setEnabled(true);
				filter.setAttribute(Author_.status);
				filter.setValue(PublicationStatus.publicationstatus_published);
				AuthorPage authorPage = new AuthorPage(filter,
						new ResourceModel(
								"p.admin.author.pageName.allPublished"));
				setResponsePage(authorPage);
			}
		});
		countPublished.setEnabled(authorService.getCount(Author_.status,
				PublicationStatus.publicationstatus_published) != 0);
		countPublished
				.add(new Label("countPublished", authorService.getCount(
						Author_.status,
						PublicationStatus.publicationstatus_published)));
		add(countPublished);
	}

	public AuthorPage() {
		filter.setEnabled(false);
		add(new Label("pageNameLabel", getPageTitle()));
		add(new AutorListPanel("autorList", filter));
	}

	public AuthorPage(Filter<Author> filter, ResourceModel pageName) {
		add(new Label("pageNameLabel", pageName));
		add(new AutorListPanel("autorList", filter));
	}

	@Override
	protected IModel<String> getPageTitle() {
		return new ResourceModel("adminmenu.authors");
	}

	@Override
	protected WebMarkupContainer getTarget() {
		return targetAutor;
	}

}
